const PlaceService = require('../services/place.service')

const placeService = new PlaceService()

class PlaceController {
  async findAll (ctx, next) {
    const query = ctx.request.query
    if (ctx.state.user.role === 'reporter') {
      query.owner = ctx.state.user._id
    } else if (ctx.state.user.role === 'auditor') {
      query.area = ctx.state.user.area
    }
    const data = await placeService.findAll(query)
    ctx.response.body = data
  }
  async findOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await placeService.findOne(id)
    ctx.response.body = data[0]
  }
  async updateOne (ctx, next) {
    const id = ctx.request.params.id
    const body = ctx.request.body
    const data = await placeService.updateOne(id, body)
    ctx.response.body = data
  }
  async deleteOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await placeService.deleteOne(id)
    ctx.response.body = data
  }
  async addOne (ctx) {
    const body = ctx.request.body
    body.owner = ctx.state.user._id
    const data = await placeService.addOne(body)
    ctx.response.body = data
  }
}

module.exports = PlaceController